package net.huashitong.ssydt.provider.task.service;


import net.huashitong.ssydt.PaginationResult;
import net.huashitong.ssydt.base.dao.LongMapper;
import net.huashitong.ssydt.base.service.AbstractLongService;
import net.huashitong.ssydt.exam.entity.SsExamQuestion;
import net.huashitong.ssydt.exceptions.ServiceException;
import net.huashitong.ssydt.provider.exam.service.SsExamQuestionService;
import net.huashitong.ssydt.provider.task.dto.CardBagDTO;
import net.huashitong.ssydt.provider.task.mapper.SsTaskCardMapper;
import net.huashitong.ssydt.security.utils.Result;
import net.huashitong.ssydt.security.utils.ResultUtils;
import net.huashitong.ssydt.task.entity.SsTaskCard;
import net.huashitong.ssydt.task.vo.CardBagInVO;
import net.huashitong.ssydt.task.vo.SsTaskCardQueryVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author <a href="mailto:yhy23456@163.com">huiyang.yu</a>
 * @since 2018.01.10
 */
@Service
@Transactional
public class SsTaskCardServiceImpl extends AbstractLongService<SsTaskCard> implements SsTaskCardService {

    private SsTaskCardMapper mapper;

    private SsExamQuestionService ssExamQuestionService;

    @Autowired
    public SsTaskCardServiceImpl(SsTaskCardMapper mapper,
                                 SsExamQuestionService ssExamQuestionService) {
        this.mapper = mapper;
        this.ssExamQuestionService = ssExamQuestionService;
    }

    @Override
    protected LongMapper<SsTaskCard> getLongMapper() {
        return mapper;
    }

    @Override
    public List<CardBagDTO> queryGroupByYearsWithMonths(CardBagInVO inVO) {
        return this.mapper.queryGroupByYearsWithMonths(inVO);
    }

    @Override
    public int countAll() {
        return this.mapper.countAll();
    }

    @Override
    public List<SsTaskCard> findByYearMonths(String yearMonths) {
        return this.mapper.findByYearMonths(yearMonths);
    }

    @Override
    public SsTaskCard getByQuestionId(Long questionId) {
        return this.mapper.getByQuestionId(questionId);
    }

    @Override
    public List<SsTaskCard> findByIdInList(List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            return new ArrayList<>();
        }
        return this.mapper.findByIdInList(ids);
    }

    @Override
    public PaginationResult<SsTaskCard> list(SsTaskCardQueryVO queryVO) {
        PaginationResult<SsTaskCard> pr = new PaginationResult<>();
        pr.setPageNumber(queryVO.getPageNumber());
        pr.setPageSize(queryVO.getPageSize());
        pr.setPageRecords(mapper.paginationCount(queryVO));
        List<SsTaskCard> ssTaskCards = this.mapper.list(queryVO);
        pr.setList(ssTaskCards);
        return pr;
    }

    @Override
    public Result<String> saveWithUpdateQuestionCard(SsTaskCard record) {
        addQuestionCard(record.getQuestionId());
        insert(record);
        return ResultUtils.getSuccessResults();
    }


    @Override
    public Result<String> updateWithUpdateQuestionCard(SsTaskCard record) {
        SsTaskCard dbRecord = selectByPrimaryKey(record.getId());
        if (!dbRecord.getQuestionId().equals(record.getQuestionId())) {
            removeQuestionCard(dbRecord.getQuestionId());
            addQuestionCard(record.getQuestionId());
        }
        updateByPrimaryKeySelective(record);
        return ResultUtils.getSuccessResults();
    }

    @Override
    public Result<String> deleteWithUpdateQuestionCard(Long id) {
        SsTaskCard record = selectByPrimaryKey(id);
        if (record != null) {
            removeQuestionCard(record.getQuestionId());
            deleteByPrimaryKey(record.getId());
        }
        return ResultUtils.getSuccessResults();
    }


    /**
     * 去除选择题卡包
     *
     * @param questionId
     */
    private void removeQuestionCard(Long questionId) {
        SsExamQuestion question = ssExamQuestionService.getById(questionId);
        if (question == null) {
            throw new ServiceException("请输入正确的选择题id");
        }
        question.setIsCard((byte) 0);
        ssExamQuestionService.update(question);
    }

    /**
     * 选择题设置为卡包
     *
     * @param questionId
     */
    private void addQuestionCard(Long questionId) {
        SsExamQuestion question = ssExamQuestionService.getById(questionId);
        if (question == null) {
            throw new ServiceException("请输入正确的选择题id");
        }
        question.setIsCard((byte) 1);
        ssExamQuestionService.update(question);
    }

}
